package com.leetcode.two_pointers;

/**
 * @author Dennis Li
 * @date 2020/7/29 13:04
 */
public class MaxArea_11 {

    public int maxArea(int[] height) {
        int n = height.length;
        int max = Integer.MIN_VALUE;
        for (int i = n - 1; i >= 0; i--) {
            for (int j = i + 1; j < n; j++) {
                int value = Math.min(height[i], height[j]) * (j - i);
                max = Math.max(max, value);
            }
        }
        return max;
    }

    static class Solution {
        public int maxArea(int[] height) {
            int len = height.length;
            int i = 0, j = len - 1, max = 0;
            while (i < j) {
                max = height[i] < height[j] ?
                        Math.max((j - i) * height[i++], max) :
                        Math.max((j - i) * height[j--], max);
            }
            return max;
        }
    }

    public int maxArea2(int[] height) {
        int n = height.length;
        int i = 0, j = n - 1, max = 0;
        // 向内收窄短板可以获取面积最大值
        while (i < j) {
            // 当左板较小时，移动左板的位置
            // 当右板较小时，移动右板的位置
            if (height[i] < height[j]) {
                max = Math.max((j - i) * height[i++], max);
            } else {
                max = Math.max((j - i) * height[j--], max);
            }
        }
        return max;
    }

}
